﻿using SewerAPI.Util;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using SewerAPI.Security;
using SewerAPI.Core;

namespace SewerAPITest
{
    
    
    /// <summary>
    ///This is a test class for LoggerTest and is intended
    ///to contain all LoggerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LoggerTest
    {
        #region Private Fields

        private TestContext testContextInstance;
        private MemoryAppender appender;
        private const string TESTMESSAGE = "This is a testmessage";

        #endregion

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes       

        [TestInitialize()]
        public void TestInitialize()
        {
            //Configure log4net
            log4net.Config.BasicConfigurator.Configure();
            //Instantiate the appender
            appender = new MemoryAppender(); 
            appender.Threshold = Level.All;
            appender.Name = "MemoryAppender Test";
            appender.ActivateOptions();
            appender.Layout = new PatternLayout("%message");
            //Append it
            SewerLogger.Instance.BaseLogger.AddAppender(appender);
        }

        [TestCleanup()]
        public void TestClean()
        {
            SewerLogger.Instance.BaseLogger.RemoveAllAppenders();
        }

        #endregion


        /// <summary>
        ///A test for Info
        ///</summary>
        [TestMethod()]
        public void InfoTest()
        {
            int previous = appender.GetEvents().Length;
            SewerLogger.Instance.Info(TESTMESSAGE);
            int current = appender.GetEvents().Length;
            //Check
            Assert.IsTrue(previous + 1 == current, "Message not logged");
        }

        /// <summary>
        ///A test for Info
        ///</summary>
        [TestMethod()]
        public void WarningTest()
        {
            int previous = appender.GetEvents().Length;
            SewerLogger.Instance.Warning(TESTMESSAGE);
            int current = appender.GetEvents().Length;
            //Check
            Assert.IsTrue(previous + 1 == current, "Message not logged");
        }

        /// <summary>
        ///A test for Info
        ///</summary>
        [TestMethod()]
        public void ErrorTest()
        {
            int previous = appender.GetEvents().Length;
            SewerLogger.Instance.Error(new Exception(TESTMESSAGE));
            int current = appender.GetEvents().Length;
            //Check
            Assert.IsTrue(previous + 1 == current, "Message not logged");
        }

        /// <summary>
        ///A test for Info
        ///</summary>
        [TestMethod()]
        public void DebugTest()
        {
            int previous = appender.GetEvents().Length;
            SewerLogger.Instance.Debug(new Exception(TESTMESSAGE));
            int current = appender.GetEvents().Length;
            //Check
            Assert.IsTrue(previous + 1 == current, "Message not logged");
        }
    }
}
